#include "LSM.h"

int main(int argc, char* argv[]){
  std::string garbage;
  double init1,init2,tol,lambda;
  std::fstream Input("Inputfile");
  getline(Input,garbage,'\n');
  Input >> init1 >> init2 >> tol >> lambda;
  Input.close();  
  LSM<2,11> lsm;
  lsm.load_data("data");
  double initial[2] = {init1,init2};
  double* result = lsm.LM(initial,tol,lambda);
  std::cout << "a1 = " << result[0] << " , " << "a2 = " << result[1] << std::endl;
  double f = lsm.f(lsm.r(result));
  double** data = lsm.get_data();
  std::ofstream os;
  os.open("result.m");
  os << "a1=" << result[0] << "," << "a2=" << result[1] << ";\n";
  os << "x=[0:0.01:1];\n";
  os << "y=[\n";
  for (int i = 0 ; i < 101 ; i++)
    os << lsm.get_func()->cal_func(i*0.01,result) << ",\n";
  os << "];\n";
  os << "x1=[0:0.1:1];\n";
  os << "y1=[\n";
  for (int i = 0 ; i < 11 ; i++)
    os << data[i][1] << ",\n";
  os << "];\n";
  os << "hold on;\n";
  os << "axis([-0.1,1.1,15,40]);\n";
  os << "plot(x,y,'b');\n" << "plot(x1,y1,'r*');";
  return 0;
}
